<?php
/**
 * Created by PhpStorm.
 * User: 24339
 * Date: 2018/10/29
 * Time: 9:32
 */

namespace app\index\controller;
use think\Db;
use think\Controller;
class Staff extends Controller
{
    public function lists(){
        $data = Db::table('staff')->alias('s')->field('s.*,c.name')->join('company c',"c.id = s.cmpid")->select();
        $this->assign('data',$data);
        return view();
    }
    public function add(){
        $data = Db::table('company')->select();
        $this->assign('data',$data);
        if(request()->isPost()){
            $staff = input('post.');
            $res = db('staff')->insert($staff);
            if($res){
                $this->success('添加员工成功','lists');
            }else{
                $this->error('添加员工失败');
            }
        }

        return view();
    }
    public function edit(){
        $data = Db::table('company')->select();
        $this->assign('data',$data);
        $id = input('id');
        $staff = db('staff')->find($id);
        $this->assign('staff',$staff);
        if(request()->isPost()){
            $stares = input('post.');
            $res = db('staff')->where('id',input('id'))->update($stares);
            if($res){
                $this->success('修改员工信息成功','lists');
            }else{
                $this->error('修改员工信息失败');
            }
        }
    return view();
    }
    public function del(){
        $id = input('id');
        $staff = db('staff')->delete($id);
        if($staff){
            $this->success('删除成功','lists');
        }else{
            $this->error('删除失败');
        }
        return view();
    }
}